package com.oracle.zebrafreelytraveling.ui.adapter;

import android.util.Log;

import com.facebook.drawee.view.SimpleDraweeView;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResBase;
import com.oracle.zebrafreelytraveling.data.bean.User;
import com.oracle.zebrafreelytraveling.data.model.Message;
import com.oracle.zebrafreelytraveling.net.HttpManager;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.base.ItemViewDelegate;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.base.ViewHolder;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;

import org.litepal.crud.DataSupport;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yydcnjjw on 2017/7/27.
 * 发送消息adapter
 */

public class MsgReceiveItemDelagate implements ItemViewDelegate<Message>{
    @Override
    public int getItemViewLayoutId() {
        return R.layout.item_chat_receive;
    }

    @Override
    public boolean isForViewType(Message item, int position) {
        if (item.getType() == Constant.TYPE_RECEIVED){
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void convert(ViewHolder holder, Message message, int position) {
        holder.setText(R.id.msg_right, message.getContent());
        SimpleDraweeView userHeadImg = holder.getView(R.id.chat_user_head);
        User receiveUser = DataSupport.find(User.class, message.getReceiveId());
        if (receiveUser != null){
            userHeadImg.setImageURI(Constant.DOWNLOAD_URL + receiveUser.getHeadImg());
            holder.setText(R.id.chat_user_name, receiveUser.getName());
        } else {
            getUser(holder, message);
        }
    }

    private void getUser(ViewHolder holder, Message message){
        HttpManager.getInstance().getService(Constant.BASE_URL)
                .getUser(message.getSendId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResBase<User>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }
                    @Override
                    public void onNext(@NonNull ResBase<User> userResBase) {
                        if (userResBase.getCode().equals(Constant.REQUEST_SUCCESS)) {
                            User user = userResBase.getData();
                            holder.setText(R.id.chat_user_name, user.getName());
                            SimpleDraweeView draweeView = holder.getView(R.id.chat_user_head);
                            draweeView.setImageURI(Constant.DOWNLOAD_URL + user.getHeadImg());
                        }
                        Log.d("msgRecelveItemGetUser", userResBase.getMessage());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("RecelveItemGetUserError", e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
